<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微信支付 - MarsPay支付系统</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #ffe60f;
            --primary-dark: #ffd700;
            --primary-light: #fff8c4;
            --text-dark: #333;
            --text-gray: #666;
            --border-color: #e8e8e8;
        }
        
        body {
            background: linear-gradient(180deg, #fff 0%, #f8f8f8 100%);
            min-height: 100vh;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }
        
        .payment-header {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
            color: var(--text-dark);
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(255, 230, 15, 0.3);
        }
        
        .payment-header h1 {
            font-size: 1.5rem;
            font-weight: 600;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        
        .wechat-icon {
            width: 40px;
            height: 40px;
            background: #07c160;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
        }
        
        .payment-container {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 15px;
        }
        
        .payment-main {
            background: white;
            border-radius: 20px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }
        
        .payment-content {
            display: flex;
            min-height: 600px;
        }
        
        /* 左侧订单信息 */
        .order-section {
            flex: 1;
            padding: 40px;
            background: linear-gradient(135deg, var(--primary-light) 0%, #fff 100%);
            border-right: 3px solid var(--primary-color);
        }
        
        .section-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-dark);
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .order-item {
            padding: 20px 0;
            border-bottom: 1px dashed var(--border-color);
        }
        
        .order-item:last-child {
            border-bottom: none;
        }
        
        .order-label {
            font-size: 0.95rem;
            color: var(--text-gray);
            margin-bottom: 8px;
        }
        
        .order-value {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--text-dark);
        }
        
        .order-no {
            font-family: 'Courier New', monospace;
            background: white;
            padding: 8px 12px;
            border-radius: 6px;
            font-size: 0.95rem;
        }
        
        .product-name {
            font-size: 1.2rem;
            color: var(--text-dark);
        }
        
        .amount-display {
            font-size: 2.5rem;
            font-weight: bold;
            color: #ff6b6b;
            font-family: 'Arial', sans-serif;
        }
        
        .steps-indicator {
            display: flex;
            justify-content: space-between;
            margin-top: 40px;
            padding-top: 30px;
            border-top: 2px dashed var(--border-color);
        }
        
        .step-item {
            text-align: center;
            flex: 1;
            position: relative;
        }
        
        .step-item:not(:last-child)::after {
            content: '';
            position: absolute;
            right: -50%;
            top: 18px;
            width: 100%;
            height: 2px;
            background: var(--primary-color);
        }
        
        .step-number {
            width: 36px;
            height: 36px;
            background: var(--primary-color);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 8px;
            font-weight: bold;
            color: var(--text-dark);
            position: relative;
            z-index: 1;
        }
        
        .step-text {
            font-size: 0.85rem;
            color: var(--text-gray);
        }
        
        /* 右侧二维码区域 */
        .qr-section {
            flex: 1;
            padding: 40px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            background: white;
        }
        
        .qr-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-dark);
            margin-bottom: 10px;
            text-align: center;
        }
        
        .qr-subtitle {
            color: var(--text-gray);
            margin-bottom: 30px;
            text-align: center;
        }
        
        .qr-wrapper {
            position: relative;
            padding: 20px;
            background: linear-gradient(135deg, #f8f8f8 0%, white 100%);
            border-radius: 20px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            margin-bottom: 25px;
        }
        
        .qr-code-img {
            width: 280px;
            height: 280px;
            border: 4px solid var(--primary-color);
            border-radius: 12px;
            padding: 8px;
            background: white;
        }
        
        .countdown-box {
            background: linear-gradient(135deg, #fff5e6 0%, white 100%);
            border: 2px solid var(--primary-color);
            border-radius: 50px;
            padding: 12px 24px;
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 30px;
        }
        
        .countdown-icon {
            font-size: 1.3rem;
            color: #ff6b6b;
        }
        
        .countdown-text {
            color: var(--text-dark);
            font-size: 0.95rem;
        }
        
        .countdown-time {
            font-size: 1.3rem;
            font-weight: bold;
            color: #ff6b6b;
            font-family: 'Courier New', monospace;
        }
        
        .action-buttons {
            width: 100%;
            max-width: 350px;
            display: grid;
            gap: 12px;
        }
        
        .btn-refresh {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
            color: var(--text-dark);
            border: none;
            padding: 14px 30px;
            font-size: 1.05rem;
            font-weight: 600;
            border-radius: 50px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(255, 230, 15, 0.4);
        }
        
        .btn-refresh:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 230, 15, 0.5);
        }
        
        .btn-back {
            background: white;
            color: var(--text-dark);
            border: 2px solid var(--border-color);
            padding: 14px 30px;
            font-size: 1.05rem;
            font-weight: 500;
            border-radius: 50px;
            transition: all 0.3s ease;
        }
        
        .btn-back:hover {
            border-color: var(--primary-color);
            background: var(--primary-light);
        }
        
        .status-alert {
            margin: 20px 40px;
            border-radius: 12px;
            animation: slideIn 0.3s ease;
        }
        
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 响应式设计 */
        @media (max-width: 992px) {
            .payment-content {
                flex-direction: column;
            }
            
            .order-section {
                border-right: none;
                border-bottom: 3px solid var(--primary-color);
            }
            
            .steps-indicator {
                flex-direction: column;
                gap: 20px;
            }
            
            .step-item::after {
                display: none;
            }
        }
        
        @media (max-width: 576px) {
            .order-section, .qr-section {
                padding: 25px 20px;
            }
            
            .qr-code-img {
                width: 220px;
                height: 220px;
            }
            
            .amount-display {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <!-- 头部 -->
    <div class="payment-header">
        <div class="container">
            <h1>
                <div class="wechat-icon">
                    <i class="bi bi-wechat"></i>
                </div>
                微信扫码支付
            </h1>
        </div>
    </div>

    <!-- 主体内容 -->
    <div class="payment-container">
        <div class="payment-main">
            <div class="payment-content">
                <!-- 左侧：订单信息 -->
                <div class="order-section">
                    <div class="section-title">
                        <i class="bi bi-receipt"></i>
                        订单信息
                    </div>
                    
                    <div class="order-item">
                        <div class="order-label">订单号</div>
                        <div class="order-value">
                            <span class="order-no" th:text="${order.orderNo}">ORDER001</span>
                        </div>
                    </div>
                    
                    <div class="order-item">
                        <div class="order-label">商品名称</div>
                        <div class="order-value product-name" th:text="${order.productName}">商品名称</div>
                    </div>
                    
                    <div class="order-item">
                        <div class="order-label">支付金额</div>
                        <div class="order-value">
                            <span class="amount-display" th:text="'¥' + ${order.amount}">¥0.00</span>
                        </div>
                    </div>
                    
                    <!-- 支付步骤 -->
                    <div class="steps-indicator">
                        <div class="step-item">
                            <div class="step-number">1</div>
                            <div class="step-text">创建订单</div>
                        </div>
                        <div class="step-item">
                            <div class="step-number">2</div>
                            <div class="step-text">扫码支付</div>
                        </div>
                        <div class="step-item">
                            <div class="step-number">3</div>
                            <div class="step-text">支付完成</div>
                        </div>
                    </div>
                </div>

                <!-- 右侧：二维码 -->
                <div class="qr-section">
                    <h3 class="qr-title">请使用微信扫描二维码</h3>
                    <p class="qr-subtitle">打开微信，扫一扫完成支付</p>
                    
                    <div class="qr-wrapper">
                        <img th:src="${qrCode}" alt="支付二维码" class="qr-code-img">
                    </div>

                    <div class="countdown-box">
                        <i class="bi bi-clock-history countdown-icon"></i>
                        <span class="countdown-text">请在</span>
                        <span class="countdown-time" id="countdown">5:00</span>
                        <span class="countdown-text">内完成支付</span>
                    </div>

                    <div class="action-buttons">
                        <button class="btn btn-refresh" onclick="checkPaymentStatus()">
                            <i class="bi bi-arrow-clockwise"></i> 刷新支付状态
                        </button>
                        <a href="/order/list" class="btn btn-back text-decoration-none text-center">
                            <i class="bi bi-list-ul"></i> 返回订单列表
                        </a>
                    </div>
                </div>
            </div>

            <!-- 状态提示 -->
            <div class="alert status-alert" id="statusMessage" style="display: none;"></div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script th:inline="javascript">
        const orderNo = /*[[${order.orderNo}]]*/ '';
        let checkInterval = null;
        let countdownInterval = null;
        let timeLeft = 300;

        function startPolling() {
            checkInterval = setInterval(function() {
                checkPaymentStatus(false);
            }, 3000);
        }

        function startCountdown() {
            countdownInterval = setInterval(function() {
                timeLeft--;
                const minutes = Math.floor(timeLeft / 60);
                const seconds = timeLeft % 60;
                document.getElementById('countdown').innerText = 
                    `${minutes}:${seconds.toString().padStart(2, '0')}`;
                
                if (timeLeft <= 0) {
                    clearInterval(countdownInterval);
                    clearInterval(checkInterval);
                    showStatus('⏰ 订单已超时，请重新下单', 'danger');
                }
            }, 1000);
        }

        function checkPaymentStatus(showLoading = true) {
            if (showLoading) {
                showStatus('🔍 正在查询支付状态...', 'info');
            }

            fetch(`/pay/query/${orderNo}`)
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        const order = data.data.order;
                        const status = order.status;
                        
                        if (status === 2) {
                            clearInterval(checkInterval);
                            clearInterval(countdownInterval);
                            showStatus('✅ 支付成功！正在跳转...', 'success');
                            setTimeout(() => {
                                window.location.href = '/pay/success';
                            }, 1500);
                        } else if (status === 3) {
                            clearInterval(checkInterval);
                            showStatus('❌ 支付失败，请重新支付', 'danger');
                        } else if (showLoading) {
                            showStatus('⏳ 等待支付中...', 'warning');
                            setTimeout(() => {
                                document.getElementById('statusMessage').style.display = 'none';
                            }, 2000);
                        }
                    } else if (showLoading) {
                        showStatus('❌ 查询失败：' + data.message, 'danger');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    if (showLoading) {
                        showStatus('❌ 查询失败，请重试', 'danger');
                    }
                });
        }

        function showStatus(message, type) {
            const statusDiv = document.getElementById('statusMessage');
            statusDiv.className = `alert alert-${type} status-alert`;
            statusDiv.innerHTML = message;
            statusDiv.style.display = 'block';
        }

        window.onload = function() {
            startPolling();
            startCountdown();
        };

        window.onbeforeunload = function() {
            if (checkInterval) clearInterval(checkInterval);
            if (countdownInterval) clearInterval(countdownInterval);
        };
    </script>
</body>
</html>
